package com.xinqi.modules.course.clazz.controller.app;

import com.xinqi.common.base.response.PageInfo;
import com.xinqi.common.base.response.Param;
import com.xinqi.common.base.response.R;
import com.xinqi.common.satoken.utils.LoginHelper;
import com.xinqi.modules.course.clazz.service.ClassGroupStudentService;
import com.xinqi.modules.course.clazz.dto.req.ClassGroupStudentCreateDTO;
import com.xinqi.modules.course.clazz.dto.req.ClassGroupStudentDeleteDTO;
import com.xinqi.modules.course.clazz.dto.req.ClassGroupStudentQueryDTO;
import com.xinqi.modules.course.clazz.dto.req.ClassGroupStudentUpdateDTO;
import com.xinqi.modules.course.clazz.dto.rsp.ClassGroupStudentResultDTO;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 班级分组人员表控制层
 *
 * @author: system
 * @date: 2023/01/17
 */
@Tag(name = "App-班级分组人员", description = "班级分组人员")
@RestController
@RequestMapping("/app/class-group-student")
@RequiredArgsConstructor
public class ClassGroupStudentAppController {

    private final ClassGroupStudentService classGroupStudentService;

    @Operation(summary = "班级分组人员表-分页")
    @GetMapping("/page")
    public R<PageInfo<ClassGroupStudentResultDTO>> page(Param pageable, ClassGroupStudentQueryDTO dto) {
        PageInfo<ClassGroupStudentResultDTO> page = classGroupStudentService.page(pageable, dto);
        return R.data(page);
    }

    @Operation(summary = "班级分组人员表-详情")
    @GetMapping("/{id}")
    public R<ClassGroupStudentResultDTO> findById(@PathVariable("id") Long id) {
        ClassGroupStudentResultDTO dto = classGroupStudentService.findById(id);
        return R.data(dto);
    }

    @Operation(summary = "添加组员")
    @PostMapping
    public R<Long> insert(@Validated @RequestBody ClassGroupStudentCreateDTO dto) {
        return R.data(classGroupStudentService.insert(dto));
    }

    @Operation(summary = "批量添加组员")
    @PostMapping("/insert-batch")
    public R<Boolean> insertBath(@Validated @RequestBody List<ClassGroupStudentCreateDTO> dto) {
        return R.data(classGroupStudentService.insertBatch(dto));
    }

    @Operation(summary = "编辑班级分组人员表")
    @PutMapping("/{id}")
    public R<Boolean> update(@PathVariable("id") Long id, @Validated @RequestBody ClassGroupStudentUpdateDTO dto) {
        dto.setId(id);
        return R.data(classGroupStudentService.updateById(dto));
    }

    /**
     * @param targetGroupId 目标分组id
     * @param sourceGroupId 源分组id
     * @param dtoList       学生集合
     * @return
     */
    @Operation(summary = "批量-移动")
    @PutMapping("/{groupId}/batch-update")
    public R<Boolean> batchUpdate(@PathVariable("groupId") Long targetGroupId, Long sourceGroupId, @Validated @RequestBody List<ClassGroupStudentUpdateDTO> dtoList) {
        return R.data(classGroupStudentService.batchUpdate(targetGroupId, sourceGroupId, dtoList));
    }

    @Operation(summary = "移动学生出组")
    @DeleteMapping("/{id}")
    public R<Boolean> delete(@PathVariable("id") Long id) {
        return R.data(classGroupStudentService.deleteByClassGroupStudentId(LoginHelper.getXqLoginUser(), id));
    }

    /**
     * @param dtoList 学生集合
     * @return
     */
    @Operation(summary = "批量-移除")
    @DeleteMapping("/batch-delete")
    public R<Boolean> batchDelete(@Validated @RequestBody List<ClassGroupStudentDeleteDTO> dtoList) {
        return R.data(classGroupStudentService.batchDelete(LoginHelper.getXqLoginUser(), dtoList));
    }

    @Operation(summary = "设置小组长")
    @PutMapping("/{id}/set-team-leader")
    public R<Boolean> setTeamLeader(@PathVariable("id") Long id, @Validated @RequestBody ClassGroupStudentUpdateDTO dto) {
        dto.setId(id);
        return R.data(classGroupStudentService.setTeamLeader(dto));
    }

    @Operation(summary = "取消小组长")
    @PutMapping("/{id}/cancel-team-leader")
    public R<Boolean> cancelTeamLeader(@PathVariable("id") Long id, @Validated @RequestBody ClassGroupStudentUpdateDTO dto) {
        dto.setId(id);
        return R.data(classGroupStudentService.cancelTeamLeader(dto));
    }

    @Operation(summary = "小组成员列表")
    @GetMapping("/{groupId}/student-list")
    public R<List<ClassGroupStudentResultDTO>> findStudentList(@PathVariable("groupId") Long groupId, ClassGroupStudentQueryDTO dto) {
        dto.setGroupId(groupId);
        List<ClassGroupStudentResultDTO> list = classGroupStudentService.findStudentList(dto);
        return R.data(list);
    }

    /**
     * 学生加入自由分组，学生输入口令，相同的口令创建一个小组
     *
     * @param dto
     * @return
     */
    @Operation(summary = "学生加入自由分组下的小组")
    @PostMapping("/join-free-team")
    public R<Boolean> joinFreeTeam(@Validated @RequestBody ClassGroupStudentCreateDTO dto) {
        return R.data(classGroupStudentService.joinFreeTeam(dto));
    }

    @Operation(summary = "学生自己退组")
    @PostMapping("/{classGroupId}/remove-self")
    public R<Integer> removeSelf(@PathVariable("classGroupId") Long classGroupId) {
        return R.data(classGroupStudentService.removeSelf(classGroupId));
    }

    @Operation(summary = "多个小组成员列表")
    @PostMapping("/student-list-by-ids")
    public R<List<ClassGroupStudentResultDTO>> findStudentListByGroupIds(@RequestBody List<Long> groupIds) {
        List<ClassGroupStudentResultDTO> list = classGroupStudentService.findStudentListByGroupIds(groupIds);
        return R.data(list);
    }

}
